package myjava.collections;

import java.util.*;

public class _ArrayList {

   public static void main(String[] args) {

      ArrayList<String> list = new ArrayList<String>();
      list.add("java");
      list.add("JavaScript");
      list.add(0, "haskell");

      for (int i = 0; i < list.size(); i++) {
         System.out.println(list.get(i));
      }

      /* itera velocemente
         add and remove lenti perche' deve ricreare l'array interno di storage */

      ArrayList<String> o = new ArrayList<String>();
      o.add("1");
      o.add("2");
      o.add("3");
      o.add("33");

      // inserisce e scifta gli esistenti
      o.add(0, "4");

      // sostituisce
      o.set(0, "ZERO");

      o.remove("33");

      boolean isIn = o.contains("ZERO");
      System.out.println("contains the string ZERO : " + isIn);

      System.out.println(o.toString());

      // # ArrayList containing Foo
      ArrayList<Foo> foos = new ArrayList<Foo>();
      foos.add(new Foo("laurent"));
      foos.add(new Foo("fabs"));
      foos.add(new Foo("luigi"));
      foos.add(new Foo("paul"));

      foos.remove(new Foo("paul"));
      
      for (Foo el : foos) {
         System.out.println(el.getName());
      }
   }
}

class Foo {

   private String name;

   public Foo(String n) {
      this.name = n;
   }

   public String getName() {
      return this.name;
   }

   @Override
   public boolean equals(Object o) {
      if (o instanceof Foo && ((Foo) o).getName().equals(this.name)) {
         return true;
      } else {
         return false;
      }
   }

   @Override
   public int hashCode() {
      return this.name.length();
   }
}